/**
 * @class 精确太阳实体
 * @author DuskyHuang 圣子
*/

import { _decorator, Component, MeshRenderer, renderer, Color } from 'cc';
import { SceneGraph, StarSpectrum } from 'const';
import { utils } from 'cfw';
import { setColorBySpectrum, setLabelStyle, spectrum2color } from 'helpers';
import { UIProxy } from 'proxies';
import { CelestialLabel } from '../ui/items/celestial-label';
const { ccclass } = _decorator;

@ccclass('cocosmos.sun-precise')
export class SunPrecise extends Component {

    protected onLoad(): void {
        const mat = this.getComponent(MeshRenderer)!.material!;
        this._pass0 = mat.passes[0];
        this._handle = this._pass0.getHandle('mainColor');
        this._createLabel();
    }

    private _pass0: renderer.Pass;
    private _handle: handle;

    public setSpectrum(spec: StarSpectrum, to?: StarSpectrum, lerp: num.ratio = 0.5): this {
        setColorBySpectrum(color, spec, this.node.name);
        if (to) {
            spectrum2color(color_to, to);
            Color.lerp(color, color, color_to, lerp);
        }
        this._pass0.setUniform(this._handle, color);
        return this;
    }

    public setAlpha(alpha: num.pct, alpha2: num.pct): this {
        color.a = alpha * 255;
        this._pass0.setUniform(this._handle, color);
        this._label.alpha = alpha2;
        return this;
    }

    public setScale(s: number): this {
        this.node.setWorldScale(s, s, s);
        return this;
    }

    public setNames(cn: string, en: string): this {
        this._label.config = { cn, en };
        return this;
    }

    public set visible(arg: boolean) {
        this.node.active = arg;
    }

    private _createLabel(): void {
        const label = CelestialLabel.claim('stars', 'Sun');
        label.camera = utils.findCamera(COSMOS, MAIN);
        label.target = this.node;
        this._label = setLabelStyle(label, 'solar', 'sun');
        label.setOffset(0, 0.15, 0);
        label.fontSize = FontSize.SECONDARY;
        this._label.syncActive = true;
    }

    private _label: CelestialLabel = null;
}

const { COSMOS, Cameras: { MAIN } } = SceneGraph;
const { FontSize } = UIProxy;
const color = Color.WHITE.clone();
const color_to = Color.WHITE.clone();